'use strict';

import React from 'react';
import { Checkbox } from 'antd';

const CheckboxGroup = Checkbox.Group;

class AntdCheckbox extends React.Component {

    constructor(props) {
        super(props);
        this.state = {

            dataSource: props.dataSource,//所有的权限
            defaultValue: props.defaultValue,//默认的权限
            prevCheckecValue: props.defaultValue,
        }
    };
    componentWillReceiveProps(props) {
        this.setState({

            dataSource: props.dataSource,
            defaultValue: props.defaultValue,
            prevCheckecValue: props.defaultValue,

        });
    };
    onChange(checkedValues){
        let changeValue = {};
        let prevCheckecValue = this.state.prevCheckecValue;
        let preLength = prevCheckecValue.length;
        let currLength = checkedValues.length;
        if(preLength < currLength){
            changeValue["value"] = checkedValues[preLength];
            changeValue["status"] = true;
        }else{
            changeValue["value"] = prevCheckecValue[currLength];
            changeValue["status"] = false;

            let filtrate = false;    //表示是否筛选成功
            for(let i=0;i<preLength;i++){
                if(filtrate){
                    break;
                }
                for(let j=i;j<currLength;j++){
                    if(prevCheckecValue[i] === checkedValues[j]){
                        break;
                    }else{
                        changeValue["value"] = prevCheckecValue[i];
                        changeValue["status"] = false;
                        filtrate = true;
                    }
                }
            }
        }
        this.setState({
            prevCheckecValue: checkedValues
        });
        this.props.onChange(changeValue);

    };



    render(){
        return (

            <CheckboxGroup
                defaultValue={this.state.defaultValue}
                onChange={this.onChange.bind(this)}
                options={this.state.dataSource}
            />

        );
    }
}


export default AntdCheckbox;

